﻿using FCT551.Base;
using FCT551.Helper;
using FCT551.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FCT551.ViewModels
{
    public class MotionDebugViewModel : NotifyPorperChanged
    {
        private MotionDebugView _motionDebugView;

        public MotionDebugViewModel(MotionDebugView motionDebugView)
        {
            _motionDebugView = motionDebugView;
            SetMotionSpeedCommand = new Command() { ExeAction = new Action<object>(SetMotionSpeedCmd) };
            SetMotionStopTimeCommand = new Command() { ExeAction = new Action<object>(SetMotionStopTimeCmd) };
            MotionRunByTimeCommand = new Command() { ExeAction = new Action<object>(MotionRunByTimeCmd) };
            MotionStartCommand = new Command() { ExeAction = new Action<object>(MotionStartCmd) };
            MotionStopCommand = new Command() { ExeAction = new Action<object>(MotionStopCmd) };
            ClearWarinigCommand = new Command() { ExeAction = new Action<object>(ClearWarinigCmd) };
            ReadWarinigCommand = new Command() { ExeAction = new Action<object>(ReadWarinigCmd) };
        }

        #region 绑定方法

        /// <summary>
        /// 读取电机状态
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ReadWarinigCmd(object obj)
        {
            string a1 = Global._YPF_Y40R_1.ReadWarining();
            string a2 = Global._YPF_Y40R_2.ReadWarining();

            Number4 = $"1:{a1},2:{a2}";
        }

        /// <summary>
        /// 清楚电机报警
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ClearWarinigCmd(object obj)
        {
            Global._YPF_Y40R_1.ClearWarining();
            Global._YPF_Y40R_2.ClearWarining();
        }

        /// <summary>
        /// 电机停止
        /// </summary>
        /// <param name="obj"></param>
        private void MotionStopCmd(object obj)
        {
            Global._YPF_Y40R_1.Stop();
            Global._YPF_Y40R_2.Stop();
        }

        /// <summary>
        /// 电机启动
        /// </summary>
        /// <param name="obj"></param>
        private void MotionStartCmd(object obj)
        {
            Global._YPF_Y40R_2.RunReverse();
            Global._YPF_Y40R_1.Run();
        }

        /// <summary>
        /// 电机按照时间运行
        /// </summary>
        /// <param name="obj"></param>
        private void MotionRunByTimeCmd(object obj)
        {
            if (int.TryParse(Number3, out int result))
            {
                int aaa = result + 100;
            }
        }

        /// <summary>
        /// 设置电机停止时间
        /// </summary>
        /// <param name="obj"></param>
        private void SetMotionStopTimeCmd(object obj)
        {
            if (int.TryParse(Number2, out int result))
            {
                Global._YPF_Y40R_1.SetStopTime(result);
                Global._YPF_Y40R_2.SetStopTime(result);
            }
        }

        /// <summary>
        /// 设置电机速度
        /// </summary>
        /// <param name="obj"></param>
        private void SetMotionSpeedCmd(object obj)
        {
            if (int.TryParse(Number1, out int result))
            {
                Global._YPF_Y40R_1.SetRunSpeed(result);
                Global._YPF_Y40R_2.SetRunSpeed(result);
            }
        }

        #endregion 绑定方法

        #region 绑定命令以及属性

        private string number1 = "150";

        public string Number1
        {
            set
            {
                number1 = value;
                this.SetProperty();
            }
            get => number1;
        }

        private string number2;

        public string Number2
        {
            set
            {
                number2 = value;
                this.SetProperty();
            }
            get => number2;
        }

        private string number3;

        public string Number3
        {
            set
            {
                number3 = value;
                this.SetProperty();
            }
            get => number3;
        }

        private string number4;

        public string Number4
        {
            set
            {
                number4 = value;
                this.SetProperty();
            }
            get => number4;
        }

        private Command _setMotionSpeedcommand;

        public Command SetMotionSpeedCommand
        {
            set
            {
                _setMotionSpeedcommand = value;
            }
            get => _setMotionSpeedcommand;
        }

        private Command _setMotionStopTimeCommand;

        public Command SetMotionStopTimeCommand
        {
            set
            {
                _setMotionStopTimeCommand = value;
            }
            get => _setMotionStopTimeCommand;
        }

        private Command _motionRunByTimeCommand;

        public Command MotionRunByTimeCommand
        {
            set
            {
                _motionRunByTimeCommand = value;
            }
            get => _motionRunByTimeCommand;
        }

        private Command _motionStartCommand;

        public Command MotionStartCommand
        {
            set
            {
                _motionStartCommand = value;
            }
            get => _motionStartCommand;
        }

        private Command _motionStopCommand;

        public Command MotionStopCommand
        {
            set
            {
                _motionStopCommand = value;
            }
            get => _motionStopCommand;
        }

        private Command _clearWarinigCommand;

        public Command ClearWarinigCommand
        {
            set
            {
                _clearWarinigCommand = value;
            }
            get => _clearWarinigCommand;
        }

        private Command readWarinigCommand;

        public Command ReadWarinigCommand
        {
            set
            {
                readWarinigCommand = value;
            }
            get => readWarinigCommand;
        }

        #endregion 绑定命令以及属性
    }
}